import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LotteryCheckService {

    public static final DateFormat df = new SimpleDateFormat("yyyyMMdd");

    private Map<String, Map<Integer, IssueResult>> dateIssueResultMap = new ConcurrentHashMap<>();


    /**
     *
     * @param issueNumber
     * @param guess
     * @return -1未开奖，0未中，1中
     */
    public int lotteryChek(int issueNumber, String guess) {
        long runLotteryTime = getRunLotteryTime(issueNumber);
        String date = df.format(new Date());
        while (true) {
            // 已经开奖
            if (System.currentTimeMillis() >= runLotteryTime) {
                Map<Integer, IssueResult> map = dateIssueResultMap.get(date);
                IssueResult issueResult = map.get(issueNumber);
                if (issueResult != null) {
                    if (guess.equalsIgnoreCase("大") && issueResult.lotteryResult.big) {
                        return 1;
                    } else if (guess.equalsIgnoreCase("小") && !issueResult.lotteryResult.big) {
                        return 1;
                    } else if (guess.equalsIgnoreCase("单") && issueResult.lotteryResult.single) {
                        return 1;
                    } else if (guess.equalsIgnoreCase("双") && !issueResult.lotteryResult.single) {
                        return 1;
                    } else {
                        return 0;
                    }
                } else {
                    sleep(50);
                }
            } else {
                if (System.currentTimeMillis() - runLotteryTime > 5 * 60 * 1000) {
                    System.out.println("还有至少五分钟才会开奖，请等待");
                    return -1;
                } else {
                    sleep(50);
                }
            }
        }
    }

    public int currentIssue() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
    }

    public long getRunLotteryTime(int issueNumber) {
        if (issueNumber > 1440 || issueNumber <= 0) {
            throw new IllegalArgumentException("期数在1-1440之间");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis() + (issueNumber - 1) * 60 * 1000;
    }


    private void sleep(int millSecond) {
        try {
            Thread.sleep(millSecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 指定期数的开奖结果
     */
    class IssueResult {
        String date;
        int issueNumber;
        LotteryResult lotteryResult;
    }

    /**
     * 彩票结果
     */
    class LotteryResult {
        boolean big;    // true为大，false为小
        boolean single; // true为单，false为双
    }

}
